package shark.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import shark.user.exception.SharkSystemException;
import shark.user.service.ISysRoleService;
import shark.user.spi.entity.SysRole;
import shark.user.spi.entity.SysUserRole;
import shark.user.spi.mapper.SysRoleMapper;
import shark.user.spi.mapper.SysUserRoleMapper;

@Slf4j
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements ISysRoleService {
    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Override
    public void setUserRole(String userId, String roleId) {
        Assert.hasText(userId, "用户关联角色是，userId不能为空！");
        Assert.hasText(roleId, "用户关联角色时，roleId不能为空！");

        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setUserId(userId);
        sysUserRole.setRoleId(roleId);

        int count = userRoleMapper.insert(sysUserRole);
        if (count <= 0) {
            throw new SharkSystemException("用户关联角色失败！插入0条数据");
        }
    }
}
